package com.fjwt.gz.mch.ctrl.group;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.db.dto.MchGroupDTO;
import com.fjwt.gz.db.dto.PointDTO;
import com.fjwt.gz.db.vo.ActivityVO;
import com.fjwt.gz.db.vo.MchGroupMchVO;
import com.fjwt.gz.db.vo.MchGroupVO;
import com.fjwt.gz.db.vo.MchInfoVO;
import com.fjwt.gz.mch.ctrl.CommonCtrl;
import com.fjwt.gz.service.impl.ActivityService;
import com.fjwt.gz.service.impl.MchGroupMchService;
import com.fjwt.gz.service.impl.MchGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 商圈管理类
 *
 */
@Slf4j
@RestController
@RequestMapping("/api/mchGroup")
public class MchGroupController extends CommonCtrl {

    @Autowired
    private MchGroupService mchGroupService;

    @Autowired
    private MchGroupMchService mchGroupMchService;

    @Autowired
    private ActivityService activityService;

    /**
     * 商圈列表
     */
    @PostMapping
    public ApiRes groupList(){
        MchGroupDTO mchGroupDTO = getObject(MchGroupDTO.class);
        log.info("C端商圈列表，请求地址：/api/mchGroup，请求参数：{}", JSONUtil.toJsonStr(mchGroupDTO));
        IPage<MchGroupVO> pages = mchGroupService.getMchGroupList(getIPage(), mchGroupDTO);
        log.info("C端商圈列表，请求地址：/api/mchGroup，返回结果：{}", JSONUtil.toJsonStr(pages));
        return ApiRes.page(pages);
    }

    /**
     * 获取商圈中商户列表
     */
    @PostMapping("/getMchInfoList")
    public ApiRes mchInfoList(){
        String groupNo = getValString("groupNo");
        PointDTO location = getVal("location", PointDTO.class);
        String mchNo = getValString("mchNo");
        String mchName = getValString("mchName");
        log.info("C端获取商圈中商户列表，请求地址：/api/mchGroup/getMchInfoList，请求参数:groupNo={},location={},mchNo={},mchName={}",groupNo,JSONUtil.toJsonStr(location),mchNo,mchName);
        IPage<MchInfoVO> list = mchGroupMchService.findMchInfoByGroupId(getIPage(),groupNo,location,mchNo,mchName);
        log.info("C端获取商圈中商户列表，请求地址：/api/mchGroup/getMchInfoList，返回结果:{}", JSONUtil.toJsonStr(list));
        return  ApiRes.page(list);
    }

    /**
     * 获取商家详情信息
     */
    @PostMapping("/getMchInfoDetail")
    public ApiRes getMchInfoDetail(){
        String mchNo = getValString("mchNo");
        log.info("C端获取商家详情信息，请求地址：/api/mchGroup/getMchInfoDetail，请求参数:{}",mchNo);
        MchInfoVO vo = mchGroupMchService.findMchInfoVO(mchNo);
        log.info("C端获取商家详情信息，请求地址：/api/mchGroup/getMchInfoDetail，返回结果:{}", JSONUtil.toJsonStr(vo));
        MchInfoVO data = new MchInfoVO();
        BeanUtils.copyProperties(vo, data);
        return ApiRes.ok(data);
    }

    /**
     * 获取商圈详情
     */
    @PostMapping("/getInfo")
    public ApiRes getInfo(){
        MchGroupDTO dto = getObject(MchGroupDTO.class);
        log.info("C端获取商家详情信息，请求地址：/api/mchGroup/getInfo，请求参数:{}",JSONUtil.toJsonStr(dto));
        MchGroupVO vo = mchGroupService.getByGroupNo(dto.getGroupNo());
        log.info("C端获取商家详情信息，请求地址：/api/mchGroup/getInfo，返回结果:{}", JSONUtil.toJsonStr(vo));
        return ApiRes.ok(vo);
    }

    /**
     * 获取商圈存在的用户
     * @return
     */
    @PostMapping("/groupMchList")
    public ApiRes groupMchList(){
        String groupNo = getValString("groupNo");
        String mchNo = getValString("mchNo");
        String mchName = getValString("mchName");
        log.info("C端获取商圈存在的用户，请求地址：/api/mchGroup/groupMchList，请求参数:groupNo={},mchNo={},mchName={}",groupNo,mchNo,mchName);
        IPage<MchGroupMchVO> list = mchGroupMchService.selectGroupMch(getIPage(),groupNo,mchNo,mchName);
        log.info("C端获取商圈存在的用户，请求地址：/api/mchGroup/groupMchList，返回结果:{}", JSONUtil.toJsonStr(list));
        return ApiRes.page(list);
    }

    /**
     * 获取附近商圈
     * @return
     */
    @PostMapping("/getNearGroup")
    public ApiRes getNearGroupList(){
        MchGroupDTO dto = getObject(MchGroupDTO.class);
        log.info("C端获取附近商圈，请求地址：/api/mchGroup/getNearGroup，请求参数:{}",JSONUtil.toJsonStr(dto));
        IPage<MchGroupVO> page = mchGroupService.findNearGroupList(getIPage(), dto.getLocation(), dto.getRadius(), dto.getAgentNo(), dto.getAgentNo2());
        log.info("C端获取附近商圈，请求地址：/api/mchGroup/getNearGroup，返回结果:{}", JSONUtil.toJsonStr(page));
        return ApiRes.page(page);
    }

    /**
     * 获取商圈的活动列表
     * @return
     */
    @PostMapping("/getGroupActivityList")
    public ApiRes getGroupActivityList(){
        String agentNo = getValString("agentNo");
        String groupNo = getValString("groupNo");
        log.info("C端获取商圈的活动列表，请求地址：/api/mchGroup/getGroupActivityList，请求参数:agentNo={},groupNo={}",agentNo,groupNo);
        IPage<ActivityVO> list = activityService.getGroupActivityList(getIPage(), agentNo, groupNo);
        log.info("C端获取商圈的活动列表，请求地址：/api/mchGroup/getGroupActivityList，返回结果:{}", JSONUtil.toJsonStr(list));
        return ApiRes.page(list);
    }

    /**
     * 获取活动详情信息
     */
    @PostMapping("/getGroupActivityDetail")
    public ApiRes getActivityDetail(){
        Long activityId = getValLong("activityId");
        log.info("C端获取活动详情信息，请求地址：/api/mchGroup/getGroupActivityDetail，请求参数:activityId={}",activityId);
        ActivityVO vo = activityService.getActivityDetail(activityId);
        log.info("C端获取活动详情信息，请求地址：/api/mchGroup/getGroupActivityDetail，返回结果:{}", JSONUtil.toJsonStr(vo));
        ActivityVO data = new ActivityVO();
        BeanUtils.copyProperties(vo, data);
        return ApiRes.ok(data);
    }
}
